﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using Sylvain.Model;
public partial class EmployeeLoggedIn : System.Web.UI.Page
{
    //Instantiation of SylvainManager Class.
    ISylvainManager objManager = new SylvainManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[AppSession.CurrentUser.ToString()] == null)
        {
            Response.Redirect("~/Employees.aspx");
        }

        if (!IsPostBack)
        {
            Employee objEmployee = (Employee)Session[AppSession.CurrentUser.ToString()];
            lblUser.Text = "Welcome, " + objEmployee.FirstName + " " + objEmployee.LastName + "!";


            try
            {
                // Display Latest Description of CEO.
                EmployeesSection objEmployeesSection = new EmployeesSection();
                objEmployeesSection = objManager.GetLatestCEOCorner((int)TagEmployees.CEO_Corner);
                if (objEmployeesSection.Description != null && objEmployeesSection.Description.Length > 0)
                {
                    lblCEO.Text = objEmployeesSection.Description;
                }
                else { imgCEO.Visible = false; }

                //Display Latest Description of What's New.
                EmployeesSection objWhatNew = new EmployeesSection();
                objWhatNew = objManager.GetLatestWhatNew((int)TagEmployees.What_New);
                if (objWhatNew.Description != null && objWhatNew.Description.Length > 0)
                {
                    lblWhatNew.Text = objWhatNew.Description;

                }
                else { imgWhatNew.Visible = false; }

            }
            catch
            {
                //lblError.Visible = true;
                //lblError.Text = "Operation could not be performed.Please try again";
            }
        }
        else
        {
            try
            {
                SelectedSearchText();
                Session[AppSession.SearchText.ToString()] = (this.Page.Master as SiteMaster).GetSearchText.Trim();
            }
            catch (Exception ex)
            {
                //Response.Write(ex.Message);
            }
        }
    }
    protected void imgCEO_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            spanDetails.Style.Add("background", "");
            this.tblRecentTasks.Visible = false;
            EmployeesSection obj = new EmployeesSection();
            obj = objManager.GetLatestCEOCorner((int)TagEmployees.CEO_Corner);
            if (obj != null)
            {
                spanDetails.InnerHtml = "<h1>" + obj.Title + "</h1><p>" + obj.Description + "</p>";
            }
            SelectedSearchText();
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
    }
    protected void imgWhatNew_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            spanDetails.Style.Add("background", "");
            this.tblRecentTasks.Visible = false;
            EmployeesSection obj = new EmployeesSection();
            obj = objManager.GetLatestWhatNew((int)TagEmployees.What_New);
            if (obj != null)
            {
                spanDetails.InnerHtml = "<h1>" + obj.Title + "</h1><p>" + obj.Description + "</p>";
            }
            SelectedSearchText();
        }
        catch { }
    }

    private void SelectedSearchText()
    {
        if ((this.Page.Master as SiteMaster).GetSearchText.Trim() != "")
        {
            Session[AppSession.SearchText.ToString()] = (this.Page.Master as SiteMaster).GetSearchText.Trim();
        }
        else if (Session[AppSession.SearchText.ToString()] != null)
        {
            (this.Page.Master as SiteMaster).GetSearchText = Session[AppSession.SearchText.ToString()].ToString();
        }
        if ((this.Page.Master as SiteMaster).GetSearchText.Trim() != "")
        {
            spanDetails.InnerHtml = spanDetails.InnerHtml.Replace((this.Page.Master as SiteMaster).GetSearchText, "<span style='background-color:yellow;'>" + (this.Page.Master as SiteMaster).GetSearchText + "</span>");
        }
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("~/Employees.aspx");
    }
}